
local has_value=basic_functions.has_value 

function kiosk.demangle_for_formspec(str)
-- copied from unified_inventory
	return string.gsub(str, "_([0-9]+)_", function (v) return string.char(v) end)
end

kiosk.add_inv=function(name,def)
	if name == nil then return end
	def.name = name
	if def.buy == nil then
		def.buy=1
	end
	if def.sell == nil then
		def.sell = 1
	end
	if def.sell > def.buy then
		def.sell = kiosk.sell_factor * def.buy
	end
	if def.stock==nil then def.stock=0 end
	if def.dug==nil then def.dug=0 end
	if def.crafted==nil then def.crafted=0 end
	
	if kiosk.inventar[name] ~= nil then kiosk.inventar[name] = nil end
	
	local dn = name:split(":")[1]
	if dn == "group" then
		print(name)
		def.is_group=1
	end
	
	kiosk.inventar[name] = def
	
end

kiosk.add_inventar=function(name,buy_value,sell_value,stock,fixed)
	if name == nil then
		return
	end
	if buy_value == nil then
		buy_value=1
	end
	if buy_value == 0 then
		buy_value=1
	end
	if sell_value == nil then
		sell_value=kiosk.sell_factor * buy_value
	end
	if sell_value > buy_value then
		sell_value=kiosk.sell_factor * buy_value
	end
	if stock == nil then
		stock = 0
	end
	
	if kiosk.inventar[name] == nil then
		local kinv = {name = name,
			sell = sell_value,
			buy = buy_value,
			stock=stock,
			dug=0,
			craft=0,
			}
		if fixed ~= nil then kinv.fixed=fixed end
		kiosk.inventar[name] = kinv
	else
		local kiosk_inv=kiosk.inventar[name]
		kiosk_inv.sell=sell_value
		kiosk_inv.buy=buy_value
		if fixed ~= nil then
			kiosk_inv.fixed = fixed
		end
	end
end

kiosk.is_drop_item=function(name)
	if kiosk.inventar[name] == nil then
		kiosk.add_inv(name,{})
	end
	kiosk.inventar[name].is_drop = 1
end

kiosk.register_craft_relation=function(input,output)
	if kiosk.inventar[input] == nil then
		kiosk.add_inv(input,{})
	end
	if kiosk.inventar[output] == nil then
		kiosk.add_inv(output,{})
	end
	local source=kiosk.inventar[input]
	local dest=kiosk.inventar[output]
	if source.dest == nil then
		source.dest = {output}
	else
		if not has_value(source.dest,output) then
			table.insert(source.dest,output)
		end
	end
	if dest.source == nil then
		dest.source = {input}
	else
		if not has_value(dest.source,input) then
			table.insert(dest.source,input)
		end
	end
end

kiosk.is_inventar=function(name)
	return kiosk.inventar[name] ~= nil
end

kiosk.get_value=function(name,value)
	if name == nil then return end
	if kiosk.inventar[name] == nil then
		kiosk.add_inventar(name,1,0.9,0)
	end
	local kinv=kiosk.inventar[name]
	local output = 0
	if kinv[value] ~= nil then
		output = kinv[value]
	end
	return output
end

kiosk.get_buy=function(name)
	return kiosk.get_value(name,"buy")
end

kiosk.get_sell=function(name)
	return kiosk.get_value(name,"sell")
end

kiosk.get_stock=function(name)
	return kiosk.get_value(name,"stock")
end

kiosk.player_sell=function(player)
	if player == nil then return end
	local playername=player:get_player_name()
	if playername == nil then return end
	if playername == "" then return end
	local player_inv=minetest.get_inventory({type="detached",name=playername.."_sell"})
	if player_inv == nil then return end
	local sell_stack=player_inv:get_stack("main",1)
	if sell_stack == nil then return end
	local sell_item=sell_stack:get_name()
	if sell_item == nil then return end
	if sell_item == "" then return end
	local sell_count = sell_stack:get_count()
	if sell_count < 1 then return end
	
	local price_single = kiosk.get_sell(sell_item)
	local price=price_single * sell_count
	xpfw.player_add_attribute(player,kiosk.account,price)
	local kinv=kiosk.inventar[sell_item]
	kinv.stock=kinv.stock+sell_count
	player_inv:set_stack("main",1,nil)
	xpfw.player_add_attribute(player,"kiosk_sold",sell_count)
end

